<?php
/**
 * Created by PhpStorm.
 * User: 86183
 * Date: 2020/5/12
 * Time: 9:09
 */

namespace app\api\controller;


use app\common\controller\Api;
use think\Request;

class Userinfo extends Api
{
    protected $noNeedLogin = ['add','edit'];
    protected $noNeedRight = '*';
    public function add(Request $request){
        if(!$request->isPost()){
            return;
        }else{
            $openid=$request->post('openid');
            $uid=db('user')->where('open_id',$openid)->value('id');
            if (!$uid){
                return json(['code'=>'400','openid不存在，请授权小程序获取openid']);
            }
            $data['uid']=$uid;

            $data['turename']=$request->post('turename');
            $data['gender']=$request->post('gender');
            $data['birthday']=$request->post('birthday');
            $data['identity']=$request->post('identity');
            $data['chest']=$request->post('chest');
            $data['waistline']=$request->post('waistline');
            $data['hipline']=$request->post('hipline');
            $data['footshot']=$request->post('footshot');
            $data['weight']=$request->post('weight');
            $data['height']=$request->post('height');
            $data['style']=$request->post('style');
            $data['color']=$request->post('color');
            $data['marital']=$request->post('marital');
            $data['nation']=$request->post('nation');
            $rule=[
                'turename'  => ['require','min'=>2,'max'=>5,'chs'],
                'gender'=>['require','in:男,女,未知'],
                'birthday'=>['require','date'],
                'chest'=>['require'],
                'waistline'=>['require'],
                'hipline'=>['require'],
                'footshot'=>['require'],
                'weight'=>['require'],
                'height'=>['require'],
                'style'=>['require'],
                'color'=>['require'],
                'marital'=>['require','in:已婚,未婚'],
                'nation'=>['require'],
            ];
            $msg=[
                'identity.length'=>'身份证号码长度18位'
            ];
            $validate=new \think\Validate($rule,$msg);
            if(!$validate->check($data)){
                return json(['code'=>400,'msg'=>$validate->getError()]);
            }else{
                //print_r($data);die;
                if(db('userinfo')->data($data)->insert()){
                    return json(['code'=>200,'msg'=>'个人信息提交成功']);
                }else{
                    return json(['code'=>400,'msg'=>'信息保存失败，请重试']);
                }
            }
        }
    }
    public function edit($openid){
        if($this->request->isPost()){
            $uid=$this->request->param('uid');
            $data=[
                'uid'=>$this->request->param('uid'),
                'turename'=>$this->request->param('turename'),
                'gender'=>$this->request->param('gender'),
                'birthday'=>$this->request->param('birthday'),
                'identity'=>$this->request->param('identity'),
                'chest'=>$this->request->param('chest'),
                'waistline'=>$this->request->param('waistline'),
                'hipline'=>$this->request->param('hipline'),
                'footshot'=>$this->request->param('footshot'),
                'weight'=>$this->request->param('weight'),
                'height'=>$this->request->param('height'),
                'style'=>$this->request->param('style'),
                'color'=>$this->request->param('color'),
                'marital'=>$this->request->param('marital'),
                'nation'=>$this->request->param('nation')
            ];
            if($uid){//如果传过来uid 那么就修改用户信息
                //$model=new \app\admin\model\Userinfo();
                $db=db('userinfo');
                $issucc=$db->where('uid','=',$uid)->update($data);
                if($issucc){
                    return json(['stauts'=>201,'msg'=>'修改成功']);
                }
            }else{//没有传过来uid 那么就拉去信息
                //1.编辑的逻辑 先查询所有的数据，展现给前端，前端修改，然后再入库
                $openid=$this->request->param('openid');
                $uid=db('user')->where('open_id','=',$openid)->value('id');
                $userinfo=db('userinfo')->where('uid','=',$uid)->find();
                if($userinfo){
                    return json(['status'=>200,'msg'=>'获取数据成功','data'=>$userinfo]);
                }else{
                    return json(['status'=>440,'msg'=>'个人资料尚未添加']);
                }
            }

        }else{
            return json(['status'=>400,'msg'=>'非法请求']);
        }
    }
}